<template>
  <div>
    <el-dialog :title="title" :visible="dialogShow" @close="cancel">
      <el-form
        :model="form"
        label-width="100px"
        :rules="rules"
        ref="form"
        class="myForm"
      >
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-col :span="16">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.timeOfEntry"
              style="width: 100%;"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-col :span="16">
            <el-select v-model="form.formOfEmployment" placeholder="请选择">
              <el-option
                :label="item.value"
                :value="item.id"
                v-for="(item, index) in hireTypeList"
                :key="index"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="form.workNumber"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <div class="fromInput">
            <el-input
              v-model="form.departmentName"
              @focus="showTree = true"
            ></el-input>
            <div class="select" v-if="showTree">
              <el-tree
                :data="departments"
                :props="defaultProps"
                :default-expand-all="true"
                :expand-on-click-node="false"
                icon-class="''"
                @node-click="handleNodeClick"
              ></el-tree>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="转正日期" prop="correctionTime">
          <el-col :span="16">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.correctionTime"
              style="width: 100%;"
            ></el-date-picker>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartmentsInfo } from '@/api/departments'
import {addEmployeesApi} from '@/api/employees'
import hireType from '@/api/constant/employees'
export default {
  props: ['item', 'dialogShow'],
  data () {
    return {
      form: {
        username: '', // 姓名
        mobile: '', // 手机
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        correctionTime: '' // 转正日期
      },
      title: '新增页面',
      rules: {
        username: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              let bool = reg.test(value)
              bool ? callback() : callback(new Error('手机格式不正确'))
            },
            trigger: 'blur'
          }
        ],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'blur' }],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'blur' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'blur' }],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        correctionTime: [{ required: true, message: '必填', trigger: 'blur' }]
      },
      departments: [],
      defaultProps: {
        label: 'name'
      },
      showTree: false,
      // 聘用形式
      hireTypeList: []
    }
  },
  methods: {
    cancel () {
      this.$emit('closedialog')
      this.form = {
        username: '', // 姓名
        mobile: '', // 手机
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        correctionTime: '' // 转正日期
      }
      this.$refs.form.resetFields()
    },
    // 提交数据
    submitForm () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          console.log(this.form);
          await addEmployeesApi(this.form)
          this.$emit('closedialog')
          this.$message.success('新增成功')
          this.form = {
                    username: '', // 姓名
                    mobile: '', // 手机
                    timeOfEntry: '', // 入职时间
                    formOfEmployment: '', // 聘用形式
                    workNumber: '', // 工号
                    departmentName: '', // 部门
                    correctionTime: '' // 转正日期
                  }
          this.$emit('refresh')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 获取部门
    async getDepartments () {
      let res = await getDepartmentsInfo()
      // console.log(res)
      this.departments = this.turnData(res.data.depts, '')
      this.hireTypeList = hireType.hireType
    },
    // 转换结构
    turnData (arr, pid) {
      let newArr = []
      arr.forEach(item => {
        if (item.pid === pid) {
          let children = this.turnData(arr, item.id)
          item.children = children
          newArr.push(item)
        }
      })
      return newArr
    },
    // 子节点点击事件
    handleNodeClick (data) {
      console.log(data)
      this.showTree = false
      this.form.departmentName = data.name
    }
  },
  created () {
    this.getDepartments()
  }
}
</script>

<style lang="scss">
.myForm {
  width: 70%;
  .fromInput {
    position: relative;
    .select {
      position: absolute;
      top: 42px;
      left: 0;
      width: 100%;
      height: 30px;
      border-radius: 5px;
      border: 1px solid #ccc;
      z-index: 9999;
    }
  }
}
</style>
